package breakthrough;

public class Move
{
	private String moveString = new String("");
	
	/**
	 * Move from another move
	 * @param m
	 */
	public Move(Move m)
	{
		this.moveString = m.toString();
	}
	
	/**
	 * Move from source and destination positions
	 * Add 1 to all coordinates because java's indexing starts from 0
	 * @param src
	 * @param dest
	 */
	public Move(PawnPosition src, PawnPosition dest)
	{
		this.moveString += letterFromColumn(src.getColumn() + 1);
		this.moveString += src.getRow() + 1;
		this.moveString += " - ";
		this.moveString += letterFromColumn(dest.getColumn() + 1);
		this.moveString += dest.getRow() + 1;
	}
	
	/**
	 * Move from move string sent by the server in one of the following formats: D6D7 or D6 - D7
	 * @param strMove
	 */
	public Move(String strMove)
	{
		strMove = strMove.trim();
		// If the given string move is of the format 'D6D7', we transform it in the format 'D6 - D7'
		if (!strMove.contains("-"))
		{
			this.moveString += strMove.charAt(0) + strMove.charAt(1) + " - " + strMove.charAt(2) + strMove.charAt(3);
		}
		else
			this.moveString = strMove;
	}
	
	/**
	 * Returns the letter corresponding to a column
	 * If column is valid returns a letter in [A-H], else returns X
	 * @param column
	 * @return 
	 */
	public char letterFromColumn(int column)
	{
		switch (column)
		{
			case 1:
				return 'A';
			case 2:
				return 'B';
			case 3:
				return 'C';
			case 4:
				return 'D';
			case 5:
				return 'E';
			case 6:
				return 'F';
			case 7:
				return 'G';
			case 8:
				return 'H';
			default:
				return 'X';
		}
	}
	
	/**
	 * Returns a column from a letter
	 * If letter is valid returns a integer in [1-8], else returns 0
	 * @param letter
	 * @return
	 */
	public int columnFromLetter(char letter)
	{
		switch (letter)
		{
			case 'A':
				return 1;
			case 'B':
				return 2;
			case 'C':
				return 3;
			case 'D':
				return 4;
			case 'E':
				return 5;
			case 'F':
				return 6;
			case 'G':
				return 7;
			case 'H':
				return 8;
			default:
				return 0;
		}
	}
	
	/**
	 * Returns the source position of the move
	 * Deduct 1 to all coordinates because java's indexing starts from 0
	 * @return PawnPosition(srcRow, srcCol)
	 */
	public PawnPosition getSourcePosition()
	{
		int row = Integer.parseInt("" + this.moveString.charAt(1)) - 1;
		int col = columnFromLetter(this.moveString.charAt(0)) - 1;
		
		return new PawnPosition(row, col);
	}
	
	/**
	 * Returns the destination position of the move
	 * Deduct 1 to all coordinates because java's indexing starts from 0
	 * @return PawnPosition(destRow, destCol)
	 */
	public PawnPosition getDestinationPosition()
	{
		int row = Integer.parseInt("" + this.moveString.charAt(6)) - 1;
		int col = columnFromLetter(this.moveString.charAt(5)) - 1;
		
		return new PawnPosition(row, col);
	}
	
	/**
	 * Checks if a move is fundamentally correct
	 * e.g.: if source different from destination
	 * @param aMove
	 * @return
	 */
	public boolean isValidMove()
	{
		if (this.getSourcePosition().equals(this.getDestinationPosition()))
			return false;
		if (this.getSourcePosition().getRow() > this.getDestinationPosition().getRow())
			return false;
		return true;
	}
	
	/**
	 * Returns the move in a string format
	 */
	public String toString()
	{
		return this.moveString;
	}
}
